<template>
  <div class="role-container">
    <el-button
      type="primary"
      size="small"
      icon="el-icon-plus"
      @click="addRloe"
    >新增角色</el-button>
    <el-table v-loading="loading" :data="tableData" border style="width: 100%">
      <el-table-column
        align="center"
        type="index"
        sortable
        label="序号"
        width="180"
      />
      <el-table-column
        align="center"
        prop="name"
        sortable
        label="角色"
        width="180"
      />
      <el-table-column
        align="center"
        prop="description"
        sortable
        label="描述"
      />
      <el-table-column align="center" label="操作">
        <!-- // 作用域插槽 -->
        <template v-slot:default="scope">
          <el-button
            size="mini"
            type="text"
            @click="allot_permission(scope.row)"
          >分配权限</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleEdit(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        :current-page="paget"
        :page-size="pagesize"
        layout="total, prev, pager, next"
        :total="total"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 修改新增弹窗 -->
    <dialogForm ref="dialogForm" />
    <!-- 分配新增弹窗 -->
    <dialogPermission ref="dialogPermission" />
  </div>
</template>

<script>
import { getRoleList, getRoleById, delRoleById } from '@/api/role'
import { getPermissionList } from '@/api/permission'
import dialogForm from './dialogForm.vue'
import dialogPermission from './dialogPermission.vue'
import { fromData } from '@/utils/index'
export default {
  name: 'Role',
  components: {
    dialogForm,
    dialogPermission
  },
  data() {
    return {
      issShow: false,
      activeName: 'first',
      tableData: [],
      page: 1, // 页码
      paget: 1,
      pagesize: 5, // 条数
      total: 0, // 总页数
      loading: false
    }
  },
  created() {
    this.getRole()
  },
  // async mounted() {
  //   const resList = await getPermissionList()
  //   this.$refs.dialogPermission.permissionList = await fromData(resList, '0')
  // },
  methods: {
    // 获取所有角色
    async getRole() {
      this.loading = true
      const res = await getRoleList({
        page: this.page,
        pagesize: this.pagesize
      })
      this.tableData = res.rows
      this.total = res.total
      setTimeout(() => {
        this.loading = false
      }, 300)
    },
    // 页码的点击事件
    handleCurrentChange(val) {
      this.page = val
      this.getRole()
    },
    // 新增点击事件
    addRloe() {
      this.$refs.dialogForm.dialogFormVisible = true
    },
    // 修改
    async handleEdit(row) {
      const res = await getRoleById({ id: row.id })
      this.$refs.dialogForm.roleForm = res
      this.$refs.dialogForm.dialogFormVisible = true
    },
    // 删除
    handleDelete(row) {
      this.$confirm('确认是否删除此角色?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await delRoleById({ id: row.id })
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          if (this.total % this.pagesize === 1) {
            this.page -= 1
          }
          this.getRole()
        })
        .catch(() => {})
    },
    // 分配权限
    async allot_permission(row) {
      // 获得权限列表
      const resList = await getPermissionList()
      this.$refs.dialogPermission.permissionList = await fromData(resList, '0')
      // 获得员工权限
      const res = await getRoleById({ id: row.id })
      this.$refs.dialogPermission.roleForm = res
      // 选中列表
      this.$refs.dialogPermission.checkList = res.permIds
      this.$refs.dialogPermission.showDialog = true
    }
  }
}
</script>

<style lang="scss">
.role-container {
  .el-checkbox-group {
    label {
      display: block;
      line-height: 40px;
      .el-checkbox__input.is-checked + .el-checkbox__label {
        color: #00a6a6;
      }
      .el-checkbox__input.is-checked .el-checkbox__inner {
        background-color: #00a6a6;
        border-color: #00a6a6;
      }
    }
  }
  .el-table {
    th {
      background-color: #fafafa;
      border-bottom: 2px solid #e8e8e8;
    }
  }
  .el-tabs__item.is-active,
  .el-tabs__item:hover,
  .el-button,
  .el-pager li.active {
    color: #00a6a6;
  }
  .el-input__inner:focus {
    border-color: #00a6a6 !important;
  }
  .el-button--primary,
  .el-dialog__title,
  .el-dialog__close {
    background-color: #00a6a6;
    color: #fff;
    border-color: transparent;
  }
  .el-tabs__active-bar,
  .el-dialog__header {
    background-color: #00a6a6;
  }
  .el-button--small,
  .el-pagination {
    margin: 10px 0;
    float: right;
  }
}
</style>
